package com.music.players;

import java.io.File;
import javazoom.jlgui.basicplayer.BasicPlayer;
import javazoom.jlgui.basicplayer.BasicPlayerException;

/**
 * @author sad
 */
public class Mp3Player implements IPlayer {

    private final BasicPlayer player;

    public Mp3Player(String path) throws BasicPlayerException {
        player = new BasicPlayer();
        player.open(new File(path));
    }

    public BasicPlayer getPlayer() {
        return player;
    }

    @Override
    public void play() {
        try {
            if (player.getStatus() == BasicPlayer.PAUSED) {
                player.resume();
            } else if (player.getStatus() == BasicPlayer.STOPPED) {
                player.play();
            }
            if (player.getStatus() == BasicPlayer.PLAYING) {
                // player.seek(0);
                player.play();
            }
        } catch (BasicPlayerException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void stop() {
        try {
            player.stop();
        } catch (BasicPlayerException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void close() {
        try {
            player.stop();
        } catch (BasicPlayerException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public PlayerStatus getStatus() {
        if (player.getStatus() == BasicPlayer.PLAYING) {
            return PlayerStatus.Play;
        } else {
            return PlayerStatus.Stop;
        }
    }

    @Override
    public void mute(boolean mute) {
        try {
            player.setGain(mute ? 0.0f : 1.0f);
        } catch (BasicPlayerException ex) {
        }
    }
}
